package com.cskaoyan.order.service.impl;

import com.cskaoyan.order.converter.OrderConverter;
import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dal.entitys.OrderItem;
import com.cskaoyan.order.dal.entitys.OrderShipping;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dal.persistence.OrderShippingMapper;
import com.cskaoyan.order.dto.OrderDetailInfo;
import com.cskaoyan.order.dto.OrderDetailResponse;
import com.cskaoyan.order.dto.OrderItemDto;
import com.cskaoyan.order.dto.OrderListResponse;
import com.cskaoyan.order.form.CreateOrderRequest;
import com.cskaoyan.order.form.OrderListRequest;
import com.cskaoyan.order.service.OrderCoreService;
import com.cskaoyan.order.service.OrderQueryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author kiggy
 * @since 2023/01/10 11:14
 */
@Service
public class OrderOtherServiceImpl implements OrderQueryService {

    private static Logger logger = LoggerFactory.getLogger(OrderOtherServiceImpl.class);

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper itemMapper;

    @Autowired
    OrderShippingMapper shippingMapper;

    @Autowired
    OrderConverter orderConverter;

    /**
     * 使用联表查询三个表，order，item，shopping
     * @param request
     * @return
     */
    @Transactional
    @Override
    public OrderListResponse orderList(OrderListRequest request) {
        PageHelper.startPage(request.getPage(), request.getSize());
        List<OrderDetailInfo> orderDetailInfo = orderMapper.selOrderList(request.getUserId(), request.getSort());

        PageInfo<OrderDetailInfo> infoPageInfo = new PageInfo<>(orderDetailInfo);
        OrderListResponse orderListResponse = new OrderListResponse();
        orderListResponse.setData(infoPageInfo.getList());
        orderListResponse.setTotal(infoPageInfo.getTotal());
        return orderListResponse;
    }

    @Transactional
    @Override
    public OrderDetailResponse orderDetail(String orderId) {
        // 获得order
        Order order = orderMapper.selectByPrimaryKey(orderId);
        Long userId = order.getUserId();

        // 获得orderShipping
        Example example = new Example(OrderShipping.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderId",orderId);
        List<OrderShipping> shippings = shippingMapper.selectByExample(example);
        OrderShipping shipping = shippings.get(0);
        // 类型转换
        OrderDetailResponse detailResponse = orderConverter.order2res(order, shipping);

        // 得到goodsList
        Example itemExample = new Example(OrderItem.class);
        Example.Criteria criteria1 = itemExample.createCriteria();
        criteria1.andEqualTo("orderId",orderId);
        List<OrderItem> orderItems = itemMapper.selectByExample(itemExample);
        // 类型转换
        List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);

        // 得到完整的details
        detailResponse.setUserId(userId);
        detailResponse.setGoodsList(orderItemDtos);

        return detailResponse;
    }
}
